<template>
  <ex-dialog width="50%" class="ex-diolog" :title="title" :visible="visible" :close-on-click-modal="false"
    @closeDialog="handleClose">
    <div class="main" v-loading="loading">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="175px" class="demo-ruleForm">
        <el-form-item label="是否开启转赠功能：" prop="status">
          <el-switch
            v-model="ruleForm.status"
            active-value="2"
            inactive-value="1"
            active-text="是"
            inactive-text="否"
            @change="changeStatus"
          >
          </el-switch>
        </el-form-item>
        <el-form-item label="转赠启用时间：" :prop="ruleForm.status == 2 ? 'date' : ''" >
          <el-date-picker
            v-model="ruleForm.date"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd" 
            @change="handleDate" 
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="持卡到达" :prop="ruleForm.status == 2 ? 'holdDay' : ''">
          <el-input v-model="ruleForm.holdDay" placeholder="请填写天数" clearable>
            <template slot="append">天后可进行转赠</template>
          </el-input>
    
        </el-form-item>
        
      </el-form>
    </div>
    <div class="button_box">
      
      <el-button type="primary"  @click="submitForm('ruleForm')">保存提交</el-button>
      <el-button @click="handleClose">取消</el-button>
    </div>
  </ex-dialog>
</template>
<script>
  // components
  import Loading from "@/components/Loading/index";
  // api
  import {
    setDonation,
    configDetail,
  } from "@/api/examplesOf";

  export default {
    components: {
      Loading,
    },
    props: {
      visible: Boolean,
      id: String,
      pathType: String,
    },
    data() {
      return {
        title:'转赠管理',
        loading: false,
        
        ruleForm: {
          id: '',
          endTime: '', //转赠结束时间
          status:"2",//转赠配置状态 1 不可转赠 2 可转赠 
          startTime:"",//	转赠开始时间 
          date: [],
          holdDay: '',//	持卡天数限制
        },
        rules: {
          status: [{
            required: true,
            message: "请输入名称",
            trigger: ["blur","change"]
          }],
          date: [{
            required: true,
            message: "请选择",
            trigger: "change"
          }],
          holdDay:[{
            required: true,
            message: "请输入天数",
            trigger: ["blur","change"]
          }],
        },
      };
    },
    computed: {},
    created() {
      this.ruleForm.id = this.id
      this.getDetail()
      
    },
    methods: {
      //获取开奖详情
      getDetail(){
        configDetail(this.id).then(response => {
          console.log('response',response)
          this.ruleForm.endTime =  response.result.endTime
          this.ruleForm.startTime =  response.result.startTime
          this.ruleForm.id =  response.result.id
          this.ruleForm.status = response.result.status
          this.ruleForm.holdDay = response.result.holdDay
          if(this.ruleForm.startTime && this.ruleForm.endTime){
            this.ruleForm.date = [this.ruleForm.startTime,this.ruleForm.endTime]
          } 
        });
      },
      //切换转赠
      changeStatus(e){
        if(e == 1){
          this.ruleForm.date = []
          this.ruleForm.startTime = ''
          this.ruleForm.endTime = ''
        }else if (e == 2){
          
        }
      },
      //取消
      handleClose(formName) {
        this.$emit("update:visible", false);
      },
      handleCancel(){
        this.handleClose()
      },
      //获取时间
      handleDate(e){
        console.log('e',e)
        this.ruleForm.startTime = e[0]
        this.ruleForm.endTime = e[1]
      },
      submitForm(formName) {
        //创建确定
        this.$refs[formName].validate((valid) => {
          if (valid) {
            var params = {
                ...this.ruleForm,
              };
              setDonation(params).then((res) => {
                this.$message({
                  message: "保存成功",
                  type: "success",
                });
                setTimeout(() => {
                  this.$emit('success')
                  this.handleClose()
                }, 1000);
              })
              .finally(() => {

              });
            

          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
     
    },
  };

</script>
<style lang="scss" scoped>
  .ex-diolog {
    ::v-deep .ex--dialog--footer {
      border: none !important;
      padding: 0 !important;
    }

    ::v-deep .ex--dialog--main {
      padding: 10px 20px !important;
    }
  }

  .main {
    padding: 20px 20px;
  }

  .from_box {
    display: flex;
    margin-bottom: 20px;
  }

  .from_box_l {
    width: 110px;
    color: #606266;
    font-size: 14px;
    font-weight: bold;
  }

  .from_box_r {
    color: #333;
    font-size: 15px;
    flex: 1;
  }

  .el_table_box {
    padding: 0 20px 40px;
    border: 1px solid #dcdfe6;
    border-radius: 4px;
    margin-bottom: 20px;
  }

  .el_table_box_h {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 0;
    border-bottom: 1px solid #f3f3f3;
    margin-bottom: 14px;
  }

  .el_table_box_h_title {
    border-left: 4px solid #ff9300;
    padding-left: 4px;
  }

  .carousel_box {
    width: 400px;
    margin-bottom: 20px;
  }

  .swiper_img {
    width: 100%;
    height: 100%;
  }

  .carousel_box_two {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
  }

  .phone_box {
    width: 100px;
    height: 100px;
    margin: 0 10px 10px 0;
  }

  .button_box {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px 0;
  }

  ::v-deep .avatar-uploader2 .el-upload {
    width: 100% !important;
  }

  .avatar-uploader2 {
    width: 228px;
    height: 128px;
    text-align: center;
    line-height: 128px;
    background-color: #fbfdff;
    border: 1px dashed #c0ccda;
  }

  .banner_img {
    width: 228px;
    height: 128px;
  }

</style>
